package com.spic.business.datauploader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spic.business.common.core.utils.DateUtil;
import com.spic.business.datauploader.domain.DataUploadStatus;
import com.spic.business.datauploader.mapper.DataUploadStatusMapper;
import com.spic.business.datauploader.service.IDataUploadStatusService;
import com.spic.business.violation.supervision.action.domain.PhotoWithWatermarkDTO;
import com.spic.common.core.utils.DateUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author : 赵祥
 * create at:  2022/9/21  10:52
 * @Description :
 **/
@Service
public class DataUploadStatusServiceImpl implements IDataUploadStatusService {
    @Autowired
    private DataUploadStatusMapper dataUploadStatusMapper;

    /**
     * 0-8  08:00-16:00  16-24
     * 三个班次
     */
    @Override
    public List<DataUploadStatus> selectUploadStatusList() throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        int currHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        Date startDate;
        Date endDate;
        if (0 <= currHour && currHour < 8) {
            startDate = simpleDateFormat.parse("00:00");
            endDate = simpleDateFormat.parse("08:00");
        } else if (8 <= currHour && currHour < 16) {
            startDate = simpleDateFormat.parse("08:00");
            endDate = simpleDateFormat.parse("16:00");
        } else {
            startDate = simpleDateFormat.parse("16:00");
            endDate = simpleDateFormat.parse("23:59");
        }
        QueryWrapper<DataUploadStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("t_date", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, new Date()));
        queryWrapper.between("t_time", startDate, endDate);
        List<DataUploadStatus> list = dataUploadStatusMapper.selectList(queryWrapper);
        if (list.size() == 0) {
            return list;
        }
        list.stream().forEach(item -> {
            String strDate = simpleDateFormat.format(item.getTtime());
            try {
                Date parseDate = simpleDateFormat.parse(strDate);
                item.setTtime(parseDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        });
        List<DataUploadStatus> arrayList = list.stream().filter(distinctByKey()).collect(Collectors.toList());

        List<DataUploadStatus> newList = new ArrayList<>();
        CollectionUtils.addAll(newList, new Object[arrayList.size()]);
        Collections.copy(newList, arrayList);
        /**
         *   "id": 6,
         *   "ttype": "SSWJ",
         *   "tdate": "2022-09-20T16:00:00.000+00:00",
         *   "ttime": "10:09",
         *   "status": 1   1成功,0失败
         */

        List<Date> dateRange = getMiBetweenDateRange(startDate, endDate);
        // 查询出来最后一个元素, 之前的补齐
        DataUploadStatus endStatus = arrayList.get(arrayList.size() - 1);
        int index = dateRange.indexOf(endStatus.getTtime());
        for (int i = 0; i <= index; i++) {
            Date date = dateRange.get(i);
            int flag = 0;
            for(int j = 0; j < arrayList.size(); j++) {
                if(date.compareTo(arrayList.get(j).getTtime()) == 0) {
                    flag = 1;
                    break;
                }
            }
            if(flag == 0) {
                DataUploadStatus status = new DataUploadStatus();
                status.setStatus(0);
                status.setTtype("SSWJ");
                status.setTtime(date);
                newList.add(status);
            }
        }
        //最后一个元素到最后一个时间节点的补默认值
        for (int i = index + 1; i < dateRange.size(); i++) {
            DataUploadStatus status = new DataUploadStatus();
            Date date = dateRange.get(i);
            status.setStatus(2);
            status.setTtype("SSWJ");
            status.setTtime(date);
            newList.add(status);
        }
        //按照时间排序
        newList = newList.stream().sorted(Comparator.comparing(DataUploadStatus::getTtime)).collect(Collectors.toList());
        return newList;
    }

    private static Predicate<DataUploadStatus> distinctByKey() {
        final Map<Date, DataUploadStatus> map = new ConcurrentHashMap<>();
        return status -> map.put(status.getTtime(), status) == null;
    }


    public List<Date> getMiBetweenDateRange(Date startDate, Date endDate) {
        Calendar tt = Calendar.getInstance();
        tt.setTime(startDate);
        Calendar t2 = Calendar.getInstance();
        t2.setTime(endDate);
        List<Date> dateList = new ArrayList<Date>();
        for (; tt.compareTo(t2) <= 0; tt.add(Calendar.MINUTE, 1)) {
            dateList.add(tt.getTime());
        }
        return dateList;
    }

    public Date nextMinute(Date currDate) {
        Calendar tt = Calendar.getInstance();
        tt.setTime(currDate);
        tt.add(Calendar.MINUTE, 1);
        return tt.getTime();
    }

    public long timeSub(Date afterTime, Date beforeTime) {
        long diff = afterTime.getTime() - beforeTime.getTime();
        long min = diff / 60 / 1000;
        return min;
    }

    public static void main(String[] args) throws ParseException {
        //获取时间段内每分钟的数据
//        System.out.println("=============获取时间段内每分钟的数据==============");
//        Date startDate = new SimpleDateFormat("HH:mm").parse("08:00");
//        Date endDate = new SimpleDateFormat("HH:mm").parse("16:00");
//        List<Date> betweenMis = getMiBetweenDateRange(startDate, endDate);
//        for (Date betweenMi : betweenMis) {
//            System.out.println(new SimpleDateFormat("HH:mm").format(betweenMi));
//        }
        System.out.println(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));

        Date date = new Date();

        System.out.println();

    }
}
